<template>
  <div>
          <div style="margin: 10px 0">
            <el-input v-model="name" style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search"></el-input>
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="waring" @click="reset">重置</el-button>
          </div>
      <div style="margin: 10px 0">
        <el-upload action="http://localhost:8088/file/upload" :show-file-list="false" accept="xlsx" :on-success="handleFileUploadSuccess" style="display: inline-block">
          <el-button type="primary" >
            上传文件
            <i class="el-icon-top"></i>
          </el-button>
        </el-upload>
        <el-button type="danger" @click="delBatch" class="ml-5">
          批量删除
          <i class="el-icon-remove-outline"></i>
        </el-button>
      </div>
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="文件名称" width="140"></el-table-column>
      <el-table-column prop="type" label="文件类型" width="120"></el-table-column>
      <el-table-column prop="size" label="文件大小"></el-table-column>
      <el-table-column label="下载">
        <template slot-scope="scope">
            <el-button type="primary" @click="download(scope.row.url)">下载</el-button>
        </template>
      </el-table-column>
      <el-table-column  label="启用">
        <template slot-scope="scope">
            <el-switch v-model="scope.row.isEnable" active-color="#13ce66" inactive-color="#ccc" @change="changeEnable(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="danger" @click="del(scope.row.id)">
            删除
            <i class="el-icon-remove-outline"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>

  </div>
</template>
  
  <script>
export default {
  name: "File",
  data() {
    return {
      name: '',
      form: {},
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      dialogVisible: false,
      multipleSelection: [],
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/file/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res => {

        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    reset() {
      this.name = '',
      this.load()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleClose() {
      this.dialogVisible = false
    },

    del(id) {
      this.$confirm('删除后无法恢复，确定删除吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.request.delete("/file/" + id).then(res => {
            if (res.code==='200') {
              this.$message.success('删除成功');
              this.load();  // 重新加载数据
            } else {
              this.$message.error('删除失败');
            }
          })
        })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      this.$confirm('删除后无法恢复，确定删除吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.request.post("/file/del/batch", ids).then(res => {
            if (res.code==='200') {
              this.$message.success('删除成功');
              this.load();  // 重新加载数据
            } else {
              this.$message.error('删除失败');
            }
          })
        })
    },
    handleFileUploadSuccess(){
      this.load()

    },
    download(url){
      window.open(url)
    },
    changeEnable(row){
      this.request.post("/file/update",row).then(res=>{
        if(res.code==='200'){
          this.$message.success('操作成功')
        }
      })
    }
  }

}
  </script>
  
  <style>
.headerBg {
  background: #eee !important;
}
</style>